<template>
  <div class="agriMonitoring">
    <!-- 搜索框 -->
    <div class="searchBox">
      <div>
        <el-select
          v-model="params.landId"
          popper-class="selectInp"
          :popper-append-to-body="false"
          class="searchInp"
          placeholder="选择地块"
          :clearable="false"
          @change="landChange"
        >
          <el-option
            v-for="item in landList"
            :key="item.landId"
            :label="item.landName"
            :value="item.landId"
          >
          </el-option>
        </el-select>
        <el-select
          v-model="params.NS"
          class="searchInp"
          placeholder="农事类型"
          clearable
          @change="NsChange"
        >
          <el-option
            v-for="item in missionTypes"
            :key="item.id"
            :label="item.name"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </div>
      <div>
        <el-date-picker
          v-model="dateValue"
          type="daterange"
          range-separator="至"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="changeDate"
        >
        </el-date-picker>
        <el-button
          class="greenBtn ml15"
          type="primary"
          icon="el-icon-plus"
          @click="add()"
          >添加农事</el-button
        >
      </div>
    </div>
    <!-- 列表 -->
    <!-- 农事记录 -->
    <div class="block">
      <div
        class="missionList"
        v-for="item in missions"
        :key="item.index"
        @click="openEdit(item)"
      >
        <img class="landRight" src="@/assets/images/landRight.png" />
        <div class="missionInfoType">{{ item.type }}</div>
        <div class="missionInfo">
          <div class="missionTime">时间：<span>{{ item.time }}</span></div>
          <div class="missionUser">操作人：{{ item.participants }}</div>
        </div>

        <!-- 防治 -->
        <div class="farmStockList" v-if="item.code === 'NS_FZ'">
          <div
            class="farmsStock"
            v-for="agriList in item.agriList"
            :key="agriList.id"
          >
            <!-- <div class="farmsStockIcon">
              <img src="../../../assets/img/huafeiicon.png" alt="" />
            </div> -->
            <div class="farmsStockName">{{ agriList.name }}</div>
            <div class="farmsStockNum">{{ agriList.num }}公斤</div>
          </div>
        </div>
        <!-- 施肥 -->
        <div class="farmStockList" v-if="item.code === 'NS_SF'" >
          <div class="farmsStock" v-for="agriList in item.agriList" :key="agriList.id" >
            <!-- <div class="farmsStockIcon">
              <img src="../../../assets/img/huafeiicon.png" alt="" />
            </div> -->
            <div class="farmsStockName">{{ agriList.name }}</div>
            <div class="farmsStockNum">{{ agriList.num }}公斤</div>
          </div>
        </div>
        <!-- 播种 -->
        <div class="missionInfo" v-if="item.code === 'NS_BZ'">
          <div class="missionTime">品种：{{ item.seedType }} {{ item.seedInfo }}</div>
          <div class="missionTime" v-if="item.seedNum !== null">
            种子数量：{{ item.seedNum }}
          </div>
          <div class="missionTime" v-if="item.seedSpacing !== null">
            种间距：{{ item.seedSpacing }}cm
          </div>
        </div>
        <div :class="!item.agriList && 'mt10'" v-if="item.picture">
          <div
            class="missionImg"
            v-for="(pic, index) in item.picture.split(',')"
            :key="index"
            @click.stop="pictureBig(pic)"
          >
            <img :src="pic" alt="" />
          </div>
        </div>
        <div class="missionTime" v-if="item.remark">备注：{{ item.remark }}</div>
        <!-- <div class="missionInfo">
          <div class="missionTime">{{ item.time }}</div>
          <div class="missionInfoType">{{ item.type }}</div>
          <div class="missionUser">操作人：{{ item.participants }}</div>
        </div>

        <div class="missionRemark">{{ item.remark }}</div>
        <div class="farmStockList" v-if="item.code === 'NS_FZ'">
          <div
            class="farmsStock"
            v-for="agriList in item.agriList"
            :key="agriList.id"
          >
            <div class="farmsStockIcon">
              <img src="../../assets/img/huafeiicon.png" alt="" />
            </div>
            <div class="farmsStockName">{{ agriList.name }}</div>
            <div class="farmsStockNum">{{ agriList.num }}公斤</div>
          </div>
        </div>
        <div class="farmStockList" v-if="item.code === 'NS_SF'">
          <div
            class="farmsStock"
            v-for="agriList in item.agriList"
            :key="agriList.id"
          >
            <div class="farmsStockIcon">
              <img src="../../assets/img/huafeiicon.png" alt="" />
            </div>
            <div class="farmsStockName">{{ agriList.name }}</div>
            <div class="farmsStockNum">{{ agriList.num }}公斤</div>
          </div>
        </div>
        <div class="seed" v-if="item.code === 'NS_BZ'">
          品种：{{ item.seedType }} {{ item.seedInfo }}
          <div class="seedNum" v-if="item.seedNum !== null">
            种子数量：{{ item.seedNum }}公斤
          </div>
          <div class="seedSpacing" v-if="item.seedSpacing !== null">
            种间距：{{ item.seedSpacing }}cm
          </div>
        </div>
        <div v-if="item.picture">
          <div
            class="missionImg"
            v-for="(pic, index) in item.picture.split(',')"
            :key="index"
            @click.stop="pictureBig(pic)"
          >
            <img :src="pic" alt="" />
          </div>
        </div> -->
      </div>

      <el-dialog :visible.sync="dialogVisible" width="800" append-to-body>
        <img
          :src="dialogImageUrl"
          style="display: block; max-width: 100%; margin: 0 auto"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getLandDetailApi, delLandInfoApi } from '@/api/agriLand'
import { getSysSonDictListApi } from '@/api/dict'
import { getAgriListByTimeApi } from '@/api/mission'
export default {
  name: 'agriMonitoring',
  data () {
    return {
      landParams: {
        baseId: 1,
        landId: ''
      },
      params: {
        baseId: 1,
        landId: '',
        NS: '',
        startTime: '',
        endTime: ''
      },
      landList: null,
      missionTypes: null,
      landStates: [
        { value: '空闲中', label: 0 },
        { value: '种植中', label: 1 }
      ],
      valueArea: '',
      currentPage: 1,
      dateValue: '',
      tableData: [],
      missions: [],
      dialogImageUrl: '',
      dialogVisible: false,
      landMissionData: []
    }
  },
  mounted () {
    this.landParams.baseId = this.$store.getters.userInfo.baseId
    this.params.baseId = this.$store.getters.userInfo.baseId
    this.getSysSonDictList()
  },

  methods: {
    // 数据处理
    changeData () {
      // console.log(this.landMissionData)
      this.missions = this.landMissionData
      // for (let i = 0; i < this.missions.length; i++) {
      //   if (
      //     this.missions[i].picture !== '' &&
      //     this.missions[i].picture !== null &&
      //     this.missions[i].picture !== undefined
      //   ) {
      //     this.missions[i].picture = this.landMissionData[i].picture
      //       .split(',')
      //       .map((picture) => picture)
      //   } else {
      //     this.missions[i].picture = []
      //   }
      // }
    },
    // 查询农事
    async getAgriListByTime (params) {
      if (params.landId === '') {
        this.$message.error('暂无地块')
      } else {
        const res = await getAgriListByTimeApi(params)
        this.landMissionData = res.data
        this.changeData()
      }
    },
    openEdit (data) {
      if (data.code === 'NS_BZ') {
        this.$message('请在地块详情中查看播种信息')
      } else if (data.code !== 'NS_BZ') {
        this.$store.dispatch('mission/updatemissionDetail', data)
        // console.log(this.missionDetail)
        this.$router.push({
          path: '/shuzhongtian/agriMissionAdd',
          query: {
            ...this.$route.query,
            landId: this.params.landId,
            baseId: this.params.baseId,
            name: data.type,
            code: data.code
          }
        })
      }
      // console.log(data)
      // this.$store.dispatch('mission/updatemissionDetail', data)
      // // console.log(this.missionDetail)
      // this.$router.push({
      //   path: '/shuzhongtian/agriMissionAdd',
      //   query: {
      //     ...this.params, pageType: '1'
      //   }
      // })
    },
    // 照片放大
    pictureBig (pic) {
      this.dialogImageUrl = pic
      this.dialogVisible = true
    },
    // 添加农事
    addLand () {
      this.$router.push({
        path: '/shuzhongtian/agriAreaAdd'
      })
    },
    // 字典
    async getSysSonDictList () {
      // 地块
      const res = await getLandDetailApi(this.landParams)
      this.landList = res.data
      // 地块类型
      const res2 = await getSysSonDictListApi('NS')
      // console.log(res2.data)
      this.missionTypes = res2.data
      if (this.landList.length !== 0) {
        this.params.landId = this.landList[0].landId
        this.$store.dispatch('mission/updateLandDetail', this.landList[0])
        this.getAgriListByTime(this.params)
      } else {
        this.$store.dispatch('mission/updateLandDetail', {})
        this.$message.error('暂无地块')
      }
    },
    // 删除按钮
    async delBtn (id) {
      const res = await delLandInfoApi(id)
      if (res.code === 200) {
        this.$message.success('删除成功')
        this.getLandInfoPage(this.params)
      } else {
        this.$message.error('删除失败')
      }
    },
    changeDate (e) {
      if (e === null) {
        this.params.startTime = ''
        this.params.endTime = ''
        this.getAgriListByTime(this.params)
      } else {
        this.params.startTime = e[0]
        this.params.endTime = e[1]
        this.getAgriListByTime(this.params)
      }
    },
    NsChange () {
      this.getAgriListByTime(this.params)
    },
    landChange () {
      this.landList.map(item => {
        if (item.landId === this.params.landId) {
          this.$store.dispatch('mission/updateLandDetail', item)
        }
      })
      this.getAgriListByTime(this.params)
    },
    // 查询
    add () {
      this.$router.push({
        path: '/shuzhongtian/agriMissionAdd',
        query: {
          ...this.params
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.agriMonitoring {
  width: 100%;
  height: 100%;
  position: relative;
  // background-color: #fff;
  .searchBox {
    height: 11vh;
    line-height: 11vh;
    background: #ffffff;
    display: flex;
    justify-content: space-between;
    .searchBox-l {
      height: 3.125vw * 0.834;
      //解决下拉框偏移
      ::v-deep .el-select > .el-input {
        height: 3.5vw;
      }
    }
    .select {
      margin-right: 1.0417vw;
      width: 250px;
      height: 3.125vw * 0.834;
      border-radius: 12px 12px 12px 12px;
    }
    .searchInp {
      width: 250px;
      height: 3.125vw * 0.834;
      margin-right: 1vw;
      ::v-deep .el-input__inner {
        background: #fff;
        border: none;
        border: 1px solid #D9D9D9;
      }
    }
  }
  .tableList {
    width: 80.2083vw;
    height: 35.4167vw * 0.834;
    overflow-y: auto;
    border-radius: 20px;
    background: #fff;
    padding: 1.0417vw;
    .addBtn {
      margin-bottom: 1.5625vw * 0.834;
    }
    .list {
      text-align: center;
    }
  }
  .pagination {
    margin-top: 1.5625vw * 0.834;
    text-align: center;
  }
}
// 任务列表
.block {
  height: 34vw;
}
.missionList {
  // height: 16.6667vw * 0.834;
  background: #FAFAFA;
  border-radius: 4px;
  padding: 15px 20px 5px 20px;
  margin-bottom: 15px;
  position: relative;
  .landRight {
    width: 30px;
    height: 30px;
    position: absolute;
    right: 60px;
    top: 0;
    bottom: 0;
    margin: auto
  }
}
.missionList:hover {
  border: 1px solid #00C586;
}
.missionInfoType {
  // width: 8.1667vw;
  font-weight: 600;
  font-size: 20px;
  color: #333333;
  margin-bottom: 10px
}
.missionInfo {
  display: flex;
  .missionUser {
    color: #999;
    font-size: 14px;
  }
}
.missionTime {
  width: 200px;
  color: #999;
  font-size: 14px;
  margin-bottom: 10px;
  span {
    color: #00C586;
    font-weight: bold;
  }
}
.missionRemark {
  font-weight: 400;
  font-size: 1.0417vw;
  color: #666666;
  margin: 1.0417vw * 0.834 0 0.5208vw * 0.834;
}
.farmStockList {
  width: 70vw;
  display: flex;
}
.farmsStock {
  margin-bottom: 10px;
  min-width: 200px;
  margin-right: 10px;
  background: #E1F5EE;
  border-radius: 4px;
  display: flex;
  padding: 5px 10px;
  justify-content: space-between;
  color: #00C586;
  font-size: 14px;
  .farmsStockIcon {
    width: 1.6667vw;
    height: 1.6667vw;
    margin: auto 1.0417vw;
  }
  .farmsStockName {
  }
  .farmsStockNum {
  }
}
.missionImg {
  margin-top: 0.5208vw * 0.834;
  width: 5.5vw;
  height: 5.5vw;
  background: #dcdedf;
  border-radius: 12px 12px 12px 12px;
  display: inline-block;
  margin-right: 0.8208vw;
  img {
    width: 5.5vw;
    height: 5.5vw;
  }
}
</style>
